package javaCode.chapter16.exit_;

/**
 * @author 徐州城
 * @version 1.0
 */
public class ThreadExit_ {
    public static void main(String[] args) throws InterruptedException  {

        T t = new T();
        t.start();

        //如果希望main线程去控制t 线程的终止，必须可以修改loop
        //让t1 退出run方法，从而终止 t线程 -> 通知方式

        //让主线程休眠10秒，再通知 t线程退出
        System.out.println("main线程休眠10s...");
        Thread.sleep(1000 * 10);

        t.setLoop(false);
    }
}

class T extends Thread{

    private boolean loop = true;

    @Override
    public void run() {

        int count = 0;
        while (loop){

            try {
                Thread.sleep(50);// 让当前线程休眠50ms
            } catch (InterruptedException e){
                e.printStackTrace();
            }

            System.out.println("T 运行中..." + (++count));
        }
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}